const {
    globalData,
    saveUserInfo
} = getApp();
Page({
  data: {
    currentTab: false,  // true的时候是账号密码 false的时候是手机验证码
    phoneOrUser: '',  // 手机或账号
    password: '',  // 密码
    phone: '',  // 手机号
    code: '',  // 验证码
    codeFlag: true,  // 是否可发验证码
    countdown: 60,  // 倒计时
    falseFlag: false, // 错误flag
    falseMsg: '*错误提示', //错误提示
    loginFlag: false, // 是否可以登录
  },
  /**
   * 登录
   * @param {*} e 
   */
  login(e) {
    // this.data.falseMsg = "用户名或密码错误";
    if(!((this.data.currentTab && this.data.phoneOrUser!=='' && this.data.password!=='')||(!this.data.currentTab && this.data.phone!=='' && this.data.code!==''))){
        return
    }
    var params = {};
    if(this.data.currentTab){
        params = {
            username: this.data.phoneOrUser,
            password: this.data.password
        }
    }else{
        params = {
            phone: this.data.phone,
            sms_code: this.data.code
        }
    }
    wx.showNavigationBarLoading()
    getApp().network.POST({
        url:"/user.php?act=act_login",
        params: params,
        noNeedCookie: true,
        noNeedToken: true,
        success: async(res)=>{
            if(this.data.currentTab){
                if(res.data == 3){
                    await saveUserInfo(res)
                    this.setData({
                        falseFlag: false
                    })
                    wx.switchTab({
                        url: '/pages/my/my',
                        success: (e)=>{
                            console.log("第一步",getCurrentPages())
                            var page = getCurrentPages().pop();
                            console.log("第二步",page)
                            if(page==undefined || page==null) return
                            // page.onLoad();
                        }
                    })
                }else{
                    globalData.isLogin = false;
                    this.setData({
                        falseFlag: true,
                        falseMsg: "用户名或密码错误"
                    })
                }
            }else{
                if(res.data.code === 0){
                    await saveUserInfo(res)
                    this.setData({
                        falseFlag: false
                    })
                    wx.switchTab({
                        url: '/pages/my/my',
                        success: (e)=>{
                            console.log("第一步",getCurrentPages())
                            var page = getCurrentPages().pop();
                            console.log("第二步",page)
                            if(page==undefined || page==null) return
                            page.onLoad();
                        }
                    })
                }else{
                    globalData.isLogin = false;
                    this.setData({
                        falseFlag: true,
                        falseMsg: res.data.msg
                    })
                }
            }
            wx.hideNavigationBarLoading()
        },
        fail: (err)=>{
            console.log(err)
            globalData.isLogin = false;
        }
    })
  },

  /**
   * input 输入
   * @param {*} e 
   */
  bindKeyInput(e) {
    let key = e.target.dataset.key
    let value = e.detail.value
    this.setData({
      [`${key}`]: value
    })
  },
  /**
   * 获取验证码
   * @param {*} e 
   */
  getCode(e) {
    // 判空
    var  re = /^1\d{10}$/;
    if (!re.test(this.data.phone)) {
      wx.showToast({
        title: '手机号格式错误',
        icon: 'error'
      })
      return
    }
    getApp().network.POST({
        url:"/new/send_code",
        params:{
            phone: this.data.phone,
            type: "login"
        },
        success: (res)=>{
            if(res.data.code === 0){
                this.setData({
                  codeFlag: false
                })
                let interval = setInterval(() => {
                  if (this.data.countdown < 1) {
                    clearInterval(interval)
                    this.setData({
                      codeFlag: true,
                      countdown: 60
                    })
                  }
                  this.setData({
                    countdown: --this.data.countdown
                  })
                }, 1000)
            }else{
                wx.showToast({
                    title: res.data.msg,
                    icon: 'error'
                })
            }
        },
        fail: (err)=>{
            console.log(err)
        }
    })
  },

  /**
   * 切换登录方式
   * @param {*} e 
   */
  changeTab(e) {
    this.setData({
      currentTab: !this.data.currentTab,
      phoneOrUser: '',
      password: '',
      phone: '',
      code: ''
    })
  },
  goReg(){
    wx.navigateTo({
        url: '/pages/register/register'
    })
  },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        var pages = getCurrentPages();
        var beforePage = pages[pages.length - 2];
        beforePage.onLoad();
    },
})
